package vn.edu.ptithcm.pim.util;

import javax.servlet.http.HttpServletRequest;

public class ParamUtil {

	public static int getInteger(HttpServletRequest request, String parameter,
			int defaultValue) {
		if (request.getParameter(parameter) != null
				&& !"".equals(request.getParameter(parameter))) {
			return Integer.parseInt(request.getParameter(parameter));
		} else {
			return defaultValue;
		}
	}

	public static long getLong(HttpServletRequest request, String parameter,
			int defaultValue) {
		if (request.getParameter(parameter) != null
				&& !"".equals(request.getParameter(parameter))) {
			return Long.parseLong(request.getParameter(parameter));
		} else {
			return defaultValue;
		}
	}

	public static String getString(HttpServletRequest request,
			String parameter, String defaultValue) {
		if (request.getParameter(parameter) != null) {
			return request.getParameter(parameter);
		} else {
			return defaultValue;
		}
	}

	public static boolean isPdfRequest(HttpServletRequest request) {
		String fileName = ParamUtil.getString(request, "fileName", "");
		int index = fileName.indexOf("pdf");
		return index == -1 ? false : true;
	}

	public static boolean isRequestFile(HttpServletRequest request) {
		String fileName = ParamUtil.getString(request, "fileName", "");
		return fileName.isEmpty() ? false : true;
	}

	public static Object getObject(HttpServletRequest request, String name,
			Object defaultValue) {
		if (request.getAttribute(name) != null) {
			return request.getAttribute(name);
		} else {
			return defaultValue;
		}
	}

	public static boolean getBoolean(HttpServletRequest request, String name,
			boolean defaultValue) {
		String val = request.getParameter(name);
		if (val != null && ("true".equals(val) || "false".equals(val))) {
			return Boolean.valueOf(val);
		} else {
			return defaultValue;
		}
	}

}
